/**
 * ColourMapUtils.java
 * colourmap
 * Copyright (C) 2010 Alan W.F. Boyd
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * You can contact the author via the google code web page for the project
 * <http://colourmap.googlecode.com>
 */

package com.googlecode.colourmap;
import java.awt.Color;

/**
 * A set of common utilities for the colourMap application
 *
 * @author Alan W.F. Boyd
 */
public class ColourMapUtils
{
	/**
	 * Determines the difference in RGB components between two colours.
	 * @param a the first colour
	 * @param b the second colour
	 * @return an array indicating the differences between the RGB components of the two colours
	 */
	public static int[] coloursDiff(Color a, Color b)
	{
		return new int[]{a.getRed() - b.getRed(), a.getGreen() - b.getGreen(), a.getBlue() - b.getBlue()};
	}
	
	/**
	 * Translates the RGB components of a colour into a single integer.
	 * @param color the colour
	 * @return a number that represents a combination of the RGB components
	 */
	public static int colourToInt(int[] color)
	{
		return (color[0]*65536
		+ color[1]*256
		+ color[2]);
	}
	
	/**
	 * Translates the RGB components of a colour into a single integer.
	 * @param colour the colour
	 * @return a number that represents a combination of the RGB components
	 */
	public static int colourToInt(Color colour)
	{
		return colourToInt(getRGBComponents(colour));
	}
	
	/**
	 * Translates an integer into RGB components of a colour.
	 * The translation is done by converting the integer into base 256.
	 * @param i the integer to translate
	 * @return the translated colour components
	 */
	public static int[] intToColourComponents(int i)
	{
		int[] result = new int[3];
		result[0] = (i & 0xFF0000) >> 16;
		result[1] = (i & 0x00FF00) >> 8;
		result[2] = (i & 0x0000FF);

		return result;
	}
	
	/**
	 * Translates an integer into a colour.
	 * The translation is done by converting the integer into components and then converted to a full colour
	 * @param i the integer to translate
	 * @return the translated colour
	 */
	public static Color intToColour(int i)
	{
		int[] result = intToColourComponents(i);
		
		return new Color(result[0], result[1], result[2]);
	}
	
	/**
	 * Returns an array of integers based on the red, green and blue components of that colour
	 * @param colour the colour to get the components of
	 * @return an array of integers providing the red, green and blue components of the supplied colour
	 */
	public static int[] getRGBComponents(Color colour)
	{
		int[] result = new int[3];
		result[0] = colour.getRed();
		result[1] = colour.getGreen();
		result[2] = colour.getBlue();
		
		return result;
	}
}
